<?php
 //商品分类控制器
Class CategoryAction extends CommonAction {

//分类列表视图

	Public function index () {
		$result = M('category')->select();
		// $cate = recursion($result, 0, 3, 31);
		$this->cate = $result;
		$this->display();
	}

	//添加顶级分类
	Public function addTopCate () {
		$this->display();
	}

	//添加子级分类
	Public function addCate () {
		$type = M('type')->select();
		$this->type = $type;
		$this->display();
	}


	//添加分类操作
	Public function runAddCate () {
		$data = array(
			'name' => $_POST['name'],
			'pid' => isset($_POST['pid']) ? (int) $_POST['pid'] : 0,
			'tid' => isset($_POST['tid']) ? (int) $_POST['tid'] : 0,
			);
		if (M('category')->data($data)->add()) {
			$this->success('添加成功', U('index'));
		} else {
			$this->error('添加失败');
		}
	}
	//删除分类
	public function delCate(){
		$id = $this->_get("id");
        if ($id) {
            $db = M("category");
            $data = array(); //返回客户端的数据
            if ($db->find("pid=" . $id)) {
                $data['stat'] = "error";
                $data['msg'] = "请删除子栏目";
            }else if ($db->table('goods')->find("cid=" . $id)) {
                $data['stat'] = "error1";
                $data['msg'] = "请先移除栏目内商品";
            } else {
                $db->where('id=$id')->delete();
                $data['stat'] = "success";
                $data['msg'] = "";
            }
            echo json_encode($data);exit;
        }
	} 
}
?>